package com.ruoyi.system.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.Mynews;
import com.ruoyi.web.controller.tool.CommonWidth;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.DeskAnswer;
import com.ruoyi.system.service.IDeskAnswerService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 答案Controller
 * 
 * @author ruoyi
 * @date 2023-06-07
 */
@RestController
@RequestMapping("/system/answer")
public class DeskAnswerController extends BaseController
{
    @Autowired
    private IDeskAnswerService deskAnswerService;

    /**
     * 查询答案列表
     */
    @PreAuthorize("@ss.hasPermi('system:answer:list')")
    @GetMapping("/list")
    public TableDataInfo list(DeskAnswer deskAnswer)
    {
        startPage();
        List<DeskAnswer> list = deskAnswerService.selectDeskAnswerList(deskAnswer);
        return getDataTable(list);
    }

    /**
     * 导出答案列表
     */
    @PreAuthorize("@ss.hasPermi('system:answer:export')")
    @Log(title = "答案", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DeskAnswer deskAnswer)
    {
        List<DeskAnswer> list = deskAnswerService.selectDeskAnswerList(deskAnswer);
        ExcelUtil<DeskAnswer> util = new ExcelUtil<DeskAnswer>(DeskAnswer.class);
        util.exportExcel(response, list, "答案数据");
    }

    /**
     * 获取答案详细信息
     */
//    @PreAuthorize("@ss.hasPermi('system:answer:query')")
//    @GetMapping(value = "/{id}")
//    public AjaxResult getInfo(@PathVariable("id") Long id)
//    {
//        return success(deskAnswerService.selectDeskAnswerById(id));
//    }

    @PreAuthorize("@ss.hasPermi('system:answer:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo2(@PathVariable("id") Long id)
    {
        Mynews mynews = new Mynews();
        DeskAnswer deskAnswer = deskAnswerService.selectDeskAnswerByType(id);
        String tmpAs = deskAnswer.getAnswer();
        int zimujiange_num = 46;
        if (tmpAs.length()>zimujiange_num) {
            mynews.setComment(CommonWidth.getStringW(tmpAs,zimujiange_num));
        }
        else {
            mynews.setComment(tmpAs);
        }

//        mynews.setComment(deskAnswer.getAnswer());
        return AjaxResult.success(mynews);
//        return success(deskAnswerService.selectDeskAnswerByType(id));
    }

    /**
     * 新增答案
     */
    @PreAuthorize("@ss.hasPermi('system:answer:add')")
    @Log(title = "答案", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DeskAnswer deskAnswer)
    {
        return toAjax(deskAnswerService.insertDeskAnswer(deskAnswer));
    }

    /**
     * 修改答案
     */
    @PreAuthorize("@ss.hasPermi('system:answer:edit')")
    @Log(title = "答案", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DeskAnswer deskAnswer)
    {
        return toAjax(deskAnswerService.updateDeskAnswer(deskAnswer));
    }

    /**
     * 删除答案
     */
    @PreAuthorize("@ss.hasPermi('system:answer:remove')")
    @Log(title = "答案", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(deskAnswerService.deleteDeskAnswerByIds(ids));
    }
}
